﻿using System.Net.Http.Json;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.WebApi.Service
{
    public class WebRBACUserService : IRBACUserService
    {
        private readonly HttpClient _http;
        public WebRBACUserService(HttpClient http)
        {
            _http = http;
        }
        public async Task<List<UserDto>> GetListAsync(string keyword)
        {
            var list = await _http.GetFromJsonAsync<List<User>>($"user") ?? new List<User>();
            return list.Select(u => new UserDto
            {
                Id = u.Id,
                Name = u.Name,
                Account = u.Account,
                Phone = u.Phone,
                Remark = u.Remark,
            }).ToList();
        }

        public async Task Delete(long id)
        {
           var content = await _http.DeleteAsync($"user/{id}");
           var code = content.EnsureSuccessStatusCode();
           if (!code.IsSuccessStatusCode)
           {
              throw new Exception($"Failed to delete user with id {id}");
           }
        }

        public async Task<UserDto?> Get(long id)
        {
            User? user = await _http.GetFromJsonAsync<User>($"Users/{id}") ?? null;            
            if (user == null)
            {
                return null;
            }
            return new UserDto
            {
                Id = user.Id,
                Name = user.Name,
                Account = user.Account,
                Phone = user.Phone,
                Remark = user.Remark,
            };
        }

        public async Task<UserRegisterDto?> GetRegister(long id)
        {
            User? user = await _http.GetFromJsonAsync<User>($"user/register/{id}");
            if (user == null)
            {
                return null;
            }
            return new UserRegisterDto
            {
                Id = user.Id,
                Name = user.Name,
                Password = user.Password,
                Account = user.Account,
                Phone = user.Phone,
                Remark = user.Remark,
            };
        }

        public async Task<UserDto> Login(UserLoginDto userLoginDto)
        {
            var endpoint = "api/Users/login";
            var url = new Uri(_http.BaseAddress!, endpoint).AbsoluteUri;

            var ok = await _http.PostAsJsonAsync(endpoint, userLoginDto);

            ok.EnsureSuccessStatusCode();
            var user = await ok.Content.ReadFromJsonAsync<User>() ?? new User();
            return new UserDto
            {
                Id = user.Id,
                Name = user.Name,
                Account = user.Account,
                Phone = user.Phone,
                Remark = user.Remark,
            };
        }

        public void ResetOriginalData()
        {
            throw new NotImplementedException();
        }

        public Task Save(UserRegisterDto input)
        {
            throw new NotImplementedException();
        }
    }
}
